<?php
$version = Zend_Registry::get('version');  

if($this->pageInfo['meta_key']){ $this->headMeta()->setName('keywords',$this->pageInfo['meta_key']);}
if($this->pageInfo['meta_desc']){ $this->headMeta()->setName('description',$this->pageInfo['meta_desc']); }
$this->headTitle()->headTitle($this->pageInfo['contact_title'],'SET')->setSeparator(' - ')->headTitle($this->pageInfo['meta_title']);
?>
<script type="text/javascript">
$(document).ready(function() {

	$('a.reset_btn').click(function() {			
		 resetFields();	
		 removeError();	
	});
	
	function openMsgDialog(msg)
	{
		$('#dialog_msg').html(msg).dialog({
			autoOpen: true,
			modal: false,
			show: 'fade',
			resizable: true,				
			buttons: [{ text: "<?php echo html_entity_decode($this->translator->translator('common_ok'), ENT_QUOTES, 'UTF-8'); ?>", click: function() { $(this).dialog('option', 'hide', 'explode').dialog("close"); } } ],
			open: function(event, ui)
				 {
					setTimeout(function() 
					{									
						if($('#' + event.target.id).dialog('isOpen'))
						{
							$('#' + event.target.id).dialog('option', 'hide', 'explode').dialog('close');										
						}
					}, 10000);
				  }
		});
	}
	
	function openLoaderDialog()
	{
		$('#dialog_container').dialog({
					autoOpen: true,
					modal: true,
					show: 'explode',
					resizable: false,				
					open: function(event, ui)
						 {
						 	$("#progressbar").progressbar({
								value:100
				
							});							
						  }
				});
	}
	
	function closeLoaderDialog()
	{
		$('#dialog_container').dialog('option','hide','explode').dialog('close');
	}
	
	
	function resetFields()
	{
		$( "#contact_us" )[ 0 ].reset();
	}
	
	function removeError()
	{
		$('#actionMessage').html('');
				
		$.each($('form').serializeArray(), function(i, field) {	
			if(field.name.match(/\[id\]$/) || field.name.match(/\[input\]$/))
			{	
				var filearr = field.name.split('[');					
				var filename = filearr[0]; 
				$('input[name="'+field.name+'"]').removeClass('ui-state-error');
				$('#'+filename+'-input_err').html('');	
			}
			else
			{			
				$('#'+field.name).removeClass('ui-state-error');
				$('#'+field.name+'_err').html('');	
			}								
		});			
	}
	
	function addError(key,errMsg)
	{
		var err_id = key+'_err';
		var field_name = key;
		$('#'+field_name).addClass('ui-state-error');		
		$('#'+err_id).append(errMsg);
	}
	
	function getFormData()
	{
		var fieldObj = {};
		var filename = '';
		var sub_filename = '';
		var filevalue = '';
		
		$.each($('#contact_us').serializeArray(), function(i, field) {	
				if(field.name.match(/\[id\]$/) || field.name.match(/\[input\]$/))
				{					
					var filearr = field.name.split('[');
					var sub_filearr = filearr[1].split(']');					
					filename = filearr[0];
					sub_filename = sub_filearr[0];					
					filevalue =  field.value;
										
					if(fieldObj[filename])
					{
						fieldObj[filename][sub_filename] = filevalue ; 
					}
					else
					{
						fieldObj[filename] = {};
						fieldObj[filename][sub_filename] =  filevalue ;	
					}					
				}
				else
				{	 					
					fieldObj[field.name] = field.value;	
				}										
		});		
		return fieldObj;
	}
	

	$('a.submit_btn').click(function() {
		removeError();
		openLoaderDialog();
		var formData = getFormData();
		
		$.ajax({
			url: "<?php echo $this->url( array('module' => 'Contact','controller' => 'View', 'action'     => 'form' ), 'default',    false) ?>",
			  	type: 'POST',
			  	data: formData,
				success: function(response){
					//alert(response);					
					var json_arr = eval("("+response+")");										
					if(json_arr.status == 'ok')
					{
						$('#actionMessage').html(succMsgDesign(json_arr.msg));	
						resetFields();																
					}
					else if(json_arr.status == 'errV')
					{
						$('#actionMessage').html(errMsgDesign("<?php echo $this->translator->translator('common_err'); ?>"));
												
						for(var i= 0; i < json_arr.msg.length; i++)
						{
							addError(json_arr.msg[i].key,json_arr.msg[i].value);							
						}
					}
					else
					{
						$('#actionMessage').html(errMsgDesign(json_arr.msg));
					}
					if(json_arr.captcha && json_arr.captcha.status && json_arr.captcha.status == 'ok')
					{
						var captcha_name = json_arr.captcha.captcha_name;
						//alert($('input[name="'+captcha_name+'[id]"]').parents('td:first').children('img:first').attr('src'));
						$('input[name="'+captcha_name+'[id]"]').parents('td:first').children('img:first').attr('src', json_arr.captcha.ImgUrl+json_arr.captcha.id+json_arr.captcha.Suffix);
						$('input[name="'+captcha_name+'[id]"]').val(json_arr.captcha.id);
						//alert($('input[name="'+captcha_name+'[id]"]').parents('td:first').children('img:first').attr('src'));
					}
					closeLoaderDialog();
				},
			error: function(xhr, status, error){
				closeLoaderDialog();				
				var msg = "Error! " + xhr.status + " " + error;
				openMsgDialog(msg);
			}
		});
	});
	
	
	
	$('#contact_country_value').change(function() {		
			
		if($('#contact_state').html())
		{ 
			$('#contact_state').html('<option value="0">Loading Data......</option>'); 
			
			if($('#contact_city').html()){ $('#contact_city').html('<option value="0">Select a city</option>'); }
			
			$.ajax({
				  url: "<?php echo $this->url( array('module'=> 'Contact','controller' => 'view', 'action'     => 'states' ), 'default',    true) ?>",
				  type: 'POST',
				  data: { id: $('#contact_country_value').val() },
				  success: function(response){
					//alert(response);
					var json_arr = eval("("+response+")");
					var option = '<option value="0">Select a state</option>';				
					if(json_arr.status == 'ok')
					{
						for(var i = 0;i<json_arr.states.length;i++)
						{							
							option += '<option value="'+json_arr.states[i].state_id+'" >'+json_arr.states[i].state_name+'</option>';
						}
						$('#contact_state').html(option);
					}
					else
					{
						$('#contact_state').html('<option value="0">'+json_arr.msg+'</option>');
					}
				  }
			});
		}
	});	
	
	$('select.contact_state').change(function() {		
		
		$('select.contact_city').html('<option value="0">Loading Data......</option>');
		if($('select.contact_state option:selected').val() == 0 || $('select.contact_state option:selected').val() == '')
		{
			$('select.contact_city').html('<option value="0">Select a city</option>');
		}
		else
		{
			$.ajax({
				  url: "<?php echo $this->url( array('module'=> 'index','controller' => 'index', 'action'     => 'cities' ), 'default',    true) ?>",
				  type: 'POST',
				  data: { id: $('select.contact_state option:selected').val() },
				  success: function(response){
					//alert(response);
					var json_arr = eval("("+response+")");
					var option = '<option value="0">Select a City</option>';				
					if(json_arr.status == 'ok')
					{
						for(var i = 0;i<json_arr.cities.length;i++)
						{
							//alert(json_arr.states[i].state_id);
							option += '<option value="'+json_arr.cities[i].city_id+'" >'+json_arr.cities[i].city+'</option>';
						}
						$('select.contact_city').html(option);
					}
					else
					{
						$('select.contact_city').html('<option value="0">'+json_arr.msg+'</option>');
					}
				  }
			});
		}
	});
});
</script>

<div class="mod">
	<div class="contact-icon">
        <h1><?php echo $this->translator->translator('contact_frontend_title'); ?></h1>
    </div>

	<div class="clear">&nbsp;</div>

	<?php
        echo $this->pageInfo['contact_desc'];
    ?>

	<div class="clear">&nbsp;</div>

    <fieldset>
        <legend><?php echo $this->translator->translator('contact_frontend_form_name'); ?></legend>
        <div id="actionMessage"></div>
        <table width="100%" border="0" cellpadding="0" cellspacing="0" class="captcha">
            <?php echo $this->ViewForm; ?>
        </table>    
	</fieldset>

    <div class="clear btn-container">
        <span class="form-button ui-widget-content-1"><a href="javascript:  void(0);" class="reset_btn"><?php echo $this->translator->translator('common_reset'); ?></a></span>
        <span class="form-button ui-widget-content-1"><a href="javascript:  void(0);" class="submit_btn"><?php echo $this->translator->translator('contact_frontend_form_button'); ?></a></span>
    </div>
</div>

<div id="dialog_msg" title="<?php echo $this->translator->translator('common_dialog_msg','Contact Page'); ?>"></div>
<div id="dialog_container" style="display:none" title="<?php echo $this->translator->translator('common_processing_language'); ?>">
	<div id="progressbar"></div>	
</div>